package com.cfh.web.json;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.util.Assert;

import com.cfh.ahead.Constant;
import com.cfh.domain.Product;
import com.cfh.domain.ProductExample;
import com.cfh.domain.ProductExample.Criteria;
import com.cfh.service.ProductService;
import com.cfh.util.Tools;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class ProductJson extends ActionSupport implements ModelDriven {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ProductJson.class);
	
	private static final long serialVersionUID = 1L;
	private ProductService productService;
	private Map root = new HashMap();
	private Product Product = new Product();
	private String[] idsToDel = new String[200];
	public ProductJson() {
		super();

	}
	private int start;
	private int limit;
	private String sort;
	private String dir;
	
	private String SERACH_CODE;
	private String SERACH_TYPE;

	/***
	 * 得到所有用户
	 */
	public String list() {
		ProductExample example = new ProductExample();
		Criteria crit = example.createCriteria();
		example.setOrderByClause(buildOrderString());
		if(Tools.hasText(SERACH_TYPE)){
			logger.debug("list()"); //$NON-NLS-1$
			crit.andPRD_TYPELike("%"+SERACH_TYPE+"%");
		}
		
		if(Tools.hasText(SERACH_CODE)){
			logger.debug("list()"); //$NON-NLS-1$
			crit.andPRD_CODELike("%"+SERACH_CODE+"%");
		}
		//add more search 
		
		List list = productService.selectByExample(example, start, limit);
		int count = 0;
		try {
			count = productService.countByExample(example);
		} catch (Exception e) {
			count = Constant.MAX_COUNT_WHEN_EXCEPTION;
			logger.warn("list()", e); //$NON-NLS-1$
		}
		root.put("total", new Integer(count));
		root.put("items", list);
		return SUCCESS;
	}

	

	public String add() {
		logger.debug("add() - add() start"); //$NON-NLS-1$
		try {
			Assert.isTrue(Product != null);
			try {
				Product.setPRD_TYPE(Product.getPRD_TYPE().toUpperCase());
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			productService.insert(Product);
			root.put("success", new Boolean(true));
			logger.debug("add() - add() end"); //$NON-NLS-1$
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public String modify() {
		logger.debug("modify() - modify() start"); //$NON-NLS-1$
		Assert.notNull(Product);
	
		int i = productService.update(Product);
		if (i > 0) {
			root.put("success", new Boolean(true));
		} else {
			root.put("success", new Boolean(false));
		}
		logger.debug("modify() - modify() end"); //$NON-NLS-1$
		return SUCCESS;
	}

	public String del() {
		logger.debug("del() - del() start"); //$NON-NLS-1$
		ProductExample example = new ProductExample();
		Criteria criteria = example.createCriteria();
		int count = 0;
		String String = null;
		for (int i = 0; i < idsToDel.length; i++) {
			if (idsToDel[i] != null) {
				int countcicle = productService.deleteByPrimaryKey(idsToDel[i]);
				count +=countcicle;
			}
		}
		root.put("success", new Boolean(true));
		root.put("count", new Integer(count));
		logger.debug("del() - del() end"); //$NON-NLS-1$
		return SUCCESS;
	}

	private String buildOrderString() {
		String toReturn = null;
		if (Tools.hasText(sort)) {
			toReturn = sort;
			if (Tools.hasText(dir)) {
				toReturn += " " + dir;
			}
		}
		return toReturn;
	}

	/***
	 * nouse
	 */
	public String execute() {

		return SUCCESS;
	}

	public Map getRoot() {
		return root;
	}

	
	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getLimit() {
		return limit;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getStart() {
		return start;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getSort() {
		return sort;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getDir() {
		return dir;
	}

	public Object getModel() {

		return Product;
	}

	public void setIdsToDel(String[] idsToDel) {
		this.idsToDel = idsToDel;
	}

	public String[] getIdsToDel() {
		return idsToDel;
	}



	public void setProductService(ProductService ProductService) {
		this.productService = ProductService;
	}



	public ProductService getProductService() {
		return productService;
	}



	public void setSERACH_CODE(String sERACH_CODE) {
		SERACH_CODE = sERACH_CODE;
	}



	public String getSERACH_CODE() {
		return SERACH_CODE;
	}



	public void setSERACH_TYPE(String sERACH_TYPE) {
		SERACH_TYPE = sERACH_TYPE;
	}



	public String getSERACH_TYPE() {
		return SERACH_TYPE;
	}

}
